﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace JasRibbonApplicatie.Views
{
    /// <summary>
    /// Interaction logic for NieuwItemBarcodeToewijzen.xaml
    /// </summary>
    public partial class NieuwItemBarcodeToewijzen : Page
    {
        public NieuwItemBarcodeToewijzen()
        {
            InitializeComponent();
            barcodeTextBox.Focus();
        }

        private void materiaalPicker_selectionChange(object sender, RoutedEventArgs e)
        {
            BarcodeInscannenGroupBox.Visibility = Visibility.Visible;
            if (materiaalPicker.selectedMateriaalType == null && materiaalPicker.selectedToebehorenType == null)
            {
                keuzeLabel.Content = "U koos: Nog niets";
                BarcodeInscannenGroupBox.Visibility = Visibility.Collapsed;
            }
            else if (materiaalPicker.selectedMateriaalType == null)
            {
                keuzeLabel.Content = "U koos: " + materiaalPicker.selectedToebehorenType.naam;
            }
            else if (materiaalPicker.selectedToebehorenType == null)
            {
                keuzeLabel.Content = "U koos: " + materiaalPicker.selectedMateriaalType.Naam;
                
            }
            barcodeTextBox.Focus();
        }

        private void barcodeTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            
            string c = barcodeTextBox.Text.ToLower();
            if (c != "")
            {
                if (c[0] == 'm' && materiaalPicker.selectedMateriaalType != null)
                {
                    if (c.Length == 5)
                    {
                        if (!Queries.StreepjescodeExists(c))
                        {
                            Queries.AddMateriaal(new materiaal()
                            {
                                defect = false,
                                ontleenbaar = true,
                                streepjescode = c,
                                materiaalType_id = materiaalPicker.selectedMateriaalType.materiaalType_id
                            });
                            ErrorLabel.Content = "Materiaal Toegevoegd!";
                        }
                        else
                        {
                            ErrorLabel.Content = "Materiaal al Toegevoegd!";
                        }
                        barcodeTextBox.Text = "";
                    }
                    else if (c.Length > 5)
                    {
                        barcodeTextBox.Text = "";
                        ErrorLabel.Content = "Te lang";
                    }
                }
                else if (c[0] == 't' && materiaalPicker.selectedToebehorenType != null)
                {
                    if (c.Length == 5)
                    {
                        if (!Queries.StreepjescodeExists(c))
                        {
                            Queries.AddToebehoren(new toebehoren()
                            {
                                defect = false,
                                streepjescode = c,
                                toebehorenType_id = materiaalPicker.selectedToebehorenType.toebehorenType_id
                            });
                            ErrorLabel.Content = "Toebehoren toegevoegd!";
                        }
                        else
                        {
                            ErrorLabel.Content = "Toebehoren al Toegevoegd!";
                        }
                        barcodeTextBox.Text = "";
                    }
                    else if (c.Length > 5)
                    {
                        barcodeTextBox.Text = "";
                        ErrorLabel.Content = "Te lang";
                    }
                }
                else
                {
                    barcodeTextBox.Text = "";
                    ErrorLabel.Content = "Materiaal aanduiden of toebehoren en een 5digit code beginnend met m of t";
                }
            }
        }
    }
}
